﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.Parameter.UserCenter;
using Kovan.Model.ViewModel.UserCenter;

namespace Kovan.DAL.UserCenter
{
    public class DActRole : IDAL<act_role>
    {
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="applicationid"></param>
        /// <returns></returns>
        public IEnumerable<act_role> GetList(int applicationid)
        {
            using (var db = new MysqlContext())
            {
                return db.act_role.Where(c => c.application_id.Value == applicationid).ToList();
            }
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<ERole> GetList(PRole param)
        {
            using (var db = new MysqlContext())
            {
                var query = from r in db.act_role
                            join a in db.act_application on r.application_id.Value equals a.application_id.Value into left
                            from cleft in left.DefaultIfEmpty()
                            orderby r.role_id descending
                            select new ERole
                            {
                                role=r,
                                application=cleft
                            };

                if (!string.IsNullOrEmpty(param.Name))
                {
                    query = query.Where(o => o.role.name.Contains(param.Name));
                }


                //总条数
                param.TotalRows = query.Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                return query.ToList();
            }
        }
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public  act_role GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.act_role.FirstOrDefault(c => c.role_id == id);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public  int Add(act_role model)
        {
            using (var db = new MysqlContext())
            {
                db.act_role.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public  int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.act_role.FirstOrDefault(a => a.role_id == id);
                db.act_role.Remove(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public  int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var roleid = long.Parse(id);
                    var model = db.act_role.FirstOrDefault(a => a.role_id == roleid);
                    if (model != null)
                    {
                        db.act_role.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public  int Update(act_role model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<act_role>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }
    }
}
